package cn.green.mng.curd;

import cn.green.core.adapt.models.field.abs.AbsOrmField;
import cn.green.core.model.QFieldModel;
import cn.green.core.scanner.store.OrmTableStore;
import cn.green.manage.curd.QFieldCurd;

import java.util.ArrayList;
import java.util.List;

/**
 * 负责字段信息表的curd操作
 */
public interface QOrmFieldCurd extends QFieldCurd {
    QFieldModel insertQFieldModel(QFieldModel model);
    QFieldModel updateQFieldModel(QFieldModel model);
    AbsOrmField insertOrmField(AbsOrmField ormField);
    AbsOrmField updateOrmField(AbsOrmField ormField);
    List<QFieldModel> findOrmFields(String source, String scheme, String table);
    List<QFieldModel> findNotOrmFields(String source, String scheme, String table);
    @Override
    default List<QFieldModel> findFieldsByTableName(String source, String scheme, String table) {
        if (OrmTableStore.get(source, scheme, table) == null) {
            return findNotOrmFields(source, scheme, table);
        } else {
            List<QFieldModel> allFields = findOrmFields(source, scheme, table);
            if (allFields == null) {
                allFields = new ArrayList<>();
            }
            List<QFieldModel> notOrmFields = findNotOrmFields(source, scheme, table);
            if (notOrmFields == null) {
                notOrmFields = new ArrayList<>();
            }
            allFields.addAll(notOrmFields);
            return allFields;
        }
    }
    @Override
    default QFieldModel insertFieldInfo(QFieldModel qFieldModel) {
        if (qFieldModel instanceof AbsOrmField) {
            return this.insertOrmField((AbsOrmField) qFieldModel);
        } else {
            return this.insertQFieldModel(qFieldModel);
        }
    }

    @Override
    default QFieldModel updateFieldInfo(QFieldModel qFieldModel) {
        if (qFieldModel instanceof AbsOrmField) {
            return this.updateOrmField((AbsOrmField) qFieldModel);
        }else {
            return this.updateQFieldModel(qFieldModel);
        }
    }

    //获取field
    QFieldModel getField(String source, String scheme, String table, String column);

}
